Skip to content

typing: improve CaptureQueriesContext and test decorators in django.t…#3233

Merged
sobolevn merged 8 commits into
typeddjango:masterfrom
01-Harshit:fix/test-util-typing-enhancement
Jul 5, 2026
Merged

typing: improve CaptureQueriesContext and test decorators in django.t…#3233
sobolevn merged 8 commits into
typeddjango:masterfrom
01-Harshit:fix/test-util-typing-enhancement

Conversation

@01-Harshit

Copy link
Copy Markdown
Contributor

…est.utils

I have made things!

This PR improves Django type stubs by strengthening type hints and replacing private API usage, while keeping runtime behavior unchanged.

Changes:

1. Private API replacement:

  • Replaced _SupportsContains with Iterable in CaptureQueriesContext to make type-checking safer.

2. Stronger callable typing:

  • Updated _DecoratedTest, _C, new_checks, deployment_checks, and filter_func to Callable[..., Any] for better Mypy and Pyright support.

These improvements preserve runtime behavior while enhancing IDE autocomplete and type safety.

Verification:
Successfully ran: python -m mypy.stubtest django.test.utils

Output: Success: no issues found in 1 module

Comment thread django-stubs/test/utils.pyi Outdated
Comment thread django-stubs/test/utils.pyi
@sobolevn sobolevn requested a review from UnknownPlatypus June 30, 2026 11:39
@01-Harshit

Copy link
Copy Markdown
Contributor Author

@UnknownPlatypus All CI checks are now passing .

I've addressed both the concerns raised:

  1. Added assert_type tests in tests/assert_type/test_utils.py
    verifying CaptureQueriesContext respects Iterable[dict[str, str]] protocol
  2. Added @override decorator to __iter__ method

Could you please re-review when you get a chance?

Comment thread django-stubs/test/utils.pyi Outdated
initial_queries: int
final_queries: int | None
def __init__(self, connection: BaseDatabaseWrapper) -> None: ...
@override

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the right solution is to remove def __iter__ here. It just copies the base class.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @sobolevn, tried that but got a mypy
abstract attribute error. Fixed it by removing
the Iterable base class and keeping __iter__
without @override. Works now!

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@sobolevn sobolevn merged commit ea6a901 into typeddjango:master Jul 5, 2026
56 checks passed
@01-Harshit

Copy link
Copy Markdown
Contributor Author

Thanks!

Thankyou for the guidance and thorough review.
Learned a lot from this contribution.
I excited to contribute more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants